﻿using PRO.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Common;
using PRO.Entity;
using PRO.Config;

namespace web.Member
{
    public partial class register : PageBase
    {
        protected string msg_nick = "昵称只能由字母数字和下划线组成，长度不能超过18个字符";
        protected string msg_username = "您的邮箱地址";
        protected string msg_pwd = "密码不能超过18个字符";
        protected string msg_repwd = "请再输入一遍上面的密码";
        protected string msg_code = "请输入4位验证码";
        protected bool IsNick = true;
        protected bool IsUserName = true;
        protected bool IsPwd = true;
        protected bool IsRePwd = true;
        protected bool IsCode = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "注册会员-" + BaseConfigs.Title;
            if (UserInfo.ID > 0)
            {
                if (CVGRequest.GetQueryString("url").IsNullOrWhiteSpace())
                    Response.Redirect("/member");
                else
                    Response.Write(CVGRequest.GetQueryString("url"));
            }
            this.txtNick.Focus();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.txtNick.Text.IsNullOrWhiteSpace())
            {
                msg_nick = "<label>请输入昵称，只能由字母数字和下划线组成，长度不能超过18个字符</label>";
                IsNick = false;
            }
            else
            {
                if (!this.txtNick.Text.IsUserName())
                {
                    msg_nick = "<label>昵称只能以字母数字和下划线组成！</label>";
                    IsNick = false;
                }
                if (this.txtNick.Text.Length > 18 || this.txtNick.Text.Length < 4)
                {
                    msg_nick = "<label>昵称长度不能少于4个字符，并且不能超过18个字符！</label>";
                    IsNick = false;
                }
            }

            if (this.txtUserName.Text.IsNullOrWhiteSpace())
            {
                msg_username = "<label>请输入邮箱地址！</label>";
                IsUserName = false;
            }
            else
            {
                if (!this.txtUserName.Text.IsEmail())
                {
                    msg_username = "<label>您输入的邮箱地址格式不正确！</label>";
                    IsUserName = false;
                }
            }

            if (this.txtPwd.Text.IsNullOrWhiteSpace())
            {
                msg_pwd = "<label>请输入登录密码！</label>";
                IsPwd = false;
            }
            else
            {
                if (this.txtPwd.Text.Length > 18 || this.txtPwd.Text.Length < 6)
                {
                    msg_pwd = "<label>密码长度必须是6-18个字符！</label>";
                    IsPwd = false;
                }
            }


            if (this.txtRePwd.Text.IsNullOrWhiteSpace())
            {
                msg_repwd = "<label>请再输入一次上面密码！</label>";
                IsRePwd = false;
            }
            else
            {
                if (this.txtRePwd.Text.Length > 18 || this.txtRePwd.Text.Length < 6)
                {
                    msg_repwd = "<label>密码长度必须是6-18个字符！</label>";
                    IsRePwd = false;
                }
            }
            if (!this.txtPwd.Text.Equals(this.txtRePwd.Text))
            {
                msg_repwd = "<label>您两次输入的密码不一致！</label>";
                IsRePwd = false;
            }
            if (this.txtVCode.Text.IsNullOrWhiteSpace())
            {
                msg_code = "<label>请输入验证码！</label>";
                IsCode = false;
            }
            else
            {
                if (Session["HHC_ValidateCode"] == null)
                {
                    msg_code = "<label>验证码已经失效，请重新输入！</label>";
                    IsCode = false;
                }
                else
                {
                    if (!this.txtVCode.Text.ToLower().Equals(Session["HHC_ValidateCode"].ToString().ToLower()))
                    {
                        msg_code = "<label>验证码输入不正确！</label>";
                        IsCode = false;
                    }
                }
            }
            if (IsNick)
                msg_nick = "";
            if (IsUserName)
                msg_username = "";
            if (IsPwd)
                msg_pwd = "";
            if (IsRePwd)
                msg_repwd = "";
            if (IsCode)
                msg_code = "";
            if (IsNick && IsUserName && IsPwd && IsRePwd && IsCode)
            {
                if (!UserLib.IsExistUser(this.txtUserName.Text))
                {
                    UserInfo ui = new UserInfo();
                    ui.Nick = this.txtNick.Text;
                    ui.UserName = this.txtUserName.Text;
                    ui.Pwd = Utils.MD5(this.txtPwd.Text);
                    ui.RegDate = DateTime.Now;
                    if(UserLib.AddUser(ui))
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('注册成功！');</script>");
                    else
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('注册失败！');</script>");     
                }
                else
                    msg_username = "<label>该邮箱已经注册过了！</label>";

            }
        }
    }
}